﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using BEGDC.CC.BL.Common;
using BEGDC.CC.BL.Common.Query;
using BEGDC.CC.BL.UserManagement;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.Entity;
using BEGDC.CC.Web.UserControls;
using BEGDC.Utility.Castle;
using BEGDC.Utility.ExceptionManagement;
using NetBpm.Util.Client;
using NetBpm.Workflow.Execution;
using NetBpm.Workflow.Execution.EComp;

namespace BEGDC.CC.Web.DicManagement
{
    /// <summary>
    /// Summary description for main.
    /// </summary>
    public class DicManagement : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button btnExport;
        protected System.Web.UI.WebControls.Label lblRecordCount;
        protected System.Web.UI.WebControls.Label lblCurrentPageIndex;
        protected System.Web.UI.WebControls.Label lblPageCount;
        protected System.Web.UI.WebControls.ImageButton btnPageFirst;
        protected System.Web.UI.WebControls.ImageButton btnPagePrev;
        protected System.Web.UI.WebControls.ImageButton btnPageNext;
        protected System.Web.UI.WebControls.ImageButton btnPageLast;
        protected System.Web.UI.WebControls.TextBox txtPage;
        protected System.Web.UI.WebControls.ImageButton btnGotoPage;

        protected System.Web.UI.WebControls.Label lbl_ItemRecordCount;
        protected System.Web.UI.WebControls.Label lbl_ItemCurrentPageIndex;
        protected System.Web.UI.WebControls.Label lbl_ItemPageCount;
        protected System.Web.UI.WebControls.ImageButton btn_ItemPageFirst;
        protected System.Web.UI.WebControls.ImageButton btn_ItemPagePrev;
        protected System.Web.UI.WebControls.ImageButton btn_ItemPageNext;
        protected System.Web.UI.WebControls.ImageButton btn_ItemPageLast;
        protected System.Web.UI.WebControls.TextBox txt_ItemPage;
        protected System.Web.UI.WebControls.ImageButton btn_ItemGotoPage;

        protected System.Web.UI.WebControls.DataGrid dgDic;
        protected System.Web.UI.WebControls.DataGrid dgDataType;
        protected System.Web.UI.WebControls.DropDownList ddlDicType;

        protected Microsoft.Web.UI.WebControls.TabStrip TabStrip1;
        protected Microsoft.Web.UI.WebControls.MultiPage MultiPage1;
        protected System.Web.UI.WebControls.TextBox txtOperation;

        protected System.Web.UI.WebControls.CheckBox chkIsSys;
        protected System.Web.UI.WebControls.TextBox txtNewTypeName;
        protected System.Web.UI.WebControls.TextBox txtItemName;
        protected System.Web.UI.WebControls.TextBox txtSortOrder;
        protected System.Web.UI.WebControls.CheckBox chkTypeIsEnabled;
        protected System.Web.UI.WebControls.CheckBox chkIsDefault;
        protected System.Web.UI.WebControls.CheckBox chkIsEnabled;
        protected System.Web.UI.WebControls.ImageButton btnNewType;
        protected System.Web.UI.WebControls.Button btnNewItem;
        protected System.Web.UI.WebControls.Label lbTypeName;
        protected System.Web.UI.WebControls.Label lbItemName;
        protected System.Web.UI.WebControls.Label lbAddItem;
        protected System.Web.UI.WebControls.Label lbSortOrder;
        protected System.Web.UI.WebControls.PlaceHolder MainBody;
        protected Microsoft.Web.UI.WebControls.TabStrip TabStrip2;
        protected Microsoft.Web.UI.WebControls.MultiPage MultiPage2;
        protected System.Web.UI.HtmlControls.HtmlGenericControl span2;
        protected System.Web.UI.WebControls.Label lbType;
        protected System.Web.UI.WebControls.Label lbAddDataType;

        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitializeGlobalization();
                BindDicType(1);
                BindDicItemList(1);
            }
            else if (this.txtOperation.Text == "Type")
            {
                //				this.BindDicType(this._CurrentPageIndex);
            }
            else if (this.txtOperation.Text == "Item")
            {
                this.BindDicItemList(this._ItemCurrentPageIndex);
            }

            txtOperation.Text = string.Empty;
        }

        private void InitializeGlobalization()
        {
            //			this.btnNewType.ImageUrl = "../images/b-Add.gif";
            //this.btnNewItem.ImageUrl = "../images/b-Add.gif";
            //			this.TabStrip1.Items[1].Text = "DataType";
            //			this.TabStrip1.Items[3].Text = "DataItem";

            //			this.lbAddDataType.Text = "Add DataType";
            this.lbType.Text = "DataType (数据类型)";
            //			this.lbTypeName.Text = "Type Name";
            //			this.chkIsSys.Text = "Is System";
            this.lbAddItem.Text = "Add DataItem (添加新数据项)";
            this.lbItemName.Text = "ItemValue (数据项值)";
            this.lbSortOrder.Text = "SortOrder (排序值)";
            this.chkIsEnabled.Text = "IsEnabled (是否可用)";
            this.chkIsDefault.Text = "IsDefault (是否默认)";

        }

        //		private IList AllDataType
        //		{
        //			set
        //			{
        //				ViewState["allDataType"] = value;
        //			}
        //			get
        //			{
        //				if(ViewState["allDataType"] == null)
        //					return null;
        //				return (IList)ViewState["allDataType"];
        //			}
        //		}
        //		private IList DataItem
        //		{
        //			set
        //			{
        //				ViewState["dataItem"] = value;
        //			}
        //			get
        //			{
        //				if(ViewState["dataItem"] == null)
        //					return null;
        //				return (IList)ViewState["dataItem"];
        //			}
        //		}

        /// <summary>
        /// 
        /// </summary>
        private string _Sorting
        {
            get
            {
                if (ViewState["Sorting"] == null) return string.Empty;

                return ViewState["Sorting"].ToString();
            }
            set { ViewState["Sorting"] = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        private string _Sorting_Item
        {
            get
            {
                if (ViewState["_Sorting_Item"] == null) return string.Empty;

                return ViewState["_Sorting_Item"].ToString();
            }
            set { ViewState["_Sorting_Item"] = value; }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnExport.Click += new EventHandler(btnExport_Click);
            this.ddlDicType.SelectedIndexChanged += new System.EventHandler(this.ddlDicType_SelectedIndexChanged);
            this.dgDic.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgDic_SortCommand);
            this.dgDic.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgDic_ItemDataBound);
            this.btn_ItemPageFirst.Click += new System.Web.UI.ImageClickEventHandler(this.btn_ItemPageFirst_Click);
            this.btn_ItemPagePrev.Click += new System.Web.UI.ImageClickEventHandler(this.btn_ItemPagePrev_Click);
            this.btn_ItemPageNext.Click += new System.Web.UI.ImageClickEventHandler(this.btn_ItemPageNext_Click);
            this.btn_ItemPageLast.Click += new System.Web.UI.ImageClickEventHandler(this.btn_ItemPageLast_Click);
            this.btn_ItemGotoPage.Click += new System.Web.UI.ImageClickEventHandler(this.btn_ItemGotoPage_Click);
            this.btnNewItem.Click += new EventHandler(btnNewItem_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        #region ---------- paging events ----------------
        private void btnPageFirst_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            this.dgDataType.CurrentPageIndex = 0;
            this.BindDicType(1);
        }

        private void btnPagePrev_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (_CurrentPageIndex == 1) return;
            this.dgDataType.CurrentPageIndex = _CurrentPageIndex - 2;
            this.BindDicType(_CurrentPageIndex - 1);
        }

        private void btnPageNext_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (_CurrentPageIndex == _PageCount) return;
            this.dgDataType.CurrentPageIndex = _CurrentPageIndex;
            this.BindDicType(_CurrentPageIndex + 1);
        }

        private void btnPageLast_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            this.dgDataType.CurrentPageIndex = _PageCount - 1;
            this.BindDicType(_PageCount);
        }

        private void btnGotoPage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (this.txtPage.Text == string.Empty)
            {
                this.txtPage.Text = "1";
            }
            int gotopage = 1;
            try
            {
                gotopage = Convert.ToInt32(this.txtPage.Text);
            }
            catch { }
            int currentpage = 0;

            if (gotopage >= _PageCount)
            {
                gotopage = _PageCount;
                currentpage = gotopage - 1;
                if (currentpage < 0) currentpage = 0;
            }
            else if (gotopage <= 0)
            {
                gotopage = 1;
            }
            else
            {
                currentpage = gotopage - 1;
            }

            this.dgDataType.CurrentPageIndex = currentpage;

            this.BindDicType(gotopage);

            this.txtPage.Text = string.Empty;
        }

        #endregion

        #region page navigation
        private int _PageCount
        {
            get
            {
                if (ViewState["PageCount"] != null) return (int)ViewState["PageCount"];
                else
                    return 0;
            }
            set
            {
                ViewState["PageCount"] = value;
            }
        }
        private int _CurrentPageIndex
        {
            get
            {
                if (ViewState["CurrentPageIndex"] != null) return (int)ViewState["CurrentPageIndex"];
                else
                    return 0;
            }
            set
            {
                ViewState["CurrentPageIndex"] = value;
            }
        }
        private int _RowCount
        {
            get
            {
                if (ViewState["RowCount"] != null) return (int)ViewState["RowCount"];
                else
                    return 0;
            }
            set
            {
                ViewState["RowCount"] = value;
            }
        }
        private void SetPageNavi(int newPage)
        {
            if (newPage <= 1)
            {
                this.btnPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_first_inact.gif";
                this.btnPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_pre_inact.gif";
                this.btnPageFirst.Attributes["disabled"] = "true";
                this.btnPagePrev.Attributes["disabled"] = "true";
            }
            else
            {
                this.btnPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_first.gif";
                this.btnPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_pre.gif";
                this.btnPageFirst.Attributes.Remove("disabled");
                this.btnPagePrev.Attributes.Remove("disabled");
            }

            if (newPage >= _PageCount)
            {
                this.btnPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_next_inact.gif";
                this.btnPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_last_inact.gif";
                this.btnPageNext.Attributes["disabled"] = "true";
                this.btnPageLast.Attributes["disabled"] = "true";
            }
            else
            {
                this.btnPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_next.gif";
                this.btnPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_last.gif";
                this.btnPageNext.Attributes.Remove("disabled");
                this.btnPageLast.Attributes.Remove("disabled");
            }

            this.lblCurrentPageIndex.Text = newPage.ToString();
            this.lblPageCount.Text = _PageCount.ToString();
            this.lblRecordCount.Text = _RowCount.ToString();

        }

        private void SetPageCount(int count, int pageIndex)
        {
            if (count > 0)
            {
                _RowCount = count;
                _PageCount = count / this.dgDataType.PageSize;
                if (count % this.dgDataType.PageSize > 0) _PageCount++;

                _CurrentPageIndex = pageIndex;
                SetPageNavi(pageIndex);
            }
            else
            {
                _RowCount = count;
                _PageCount = count;
                _CurrentPageIndex = 0;
                SetPageNavi(0);
            }
        }

        #endregion

        #region ---------- paging events item ----------------
        private void btn_ItemPageFirst_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            this.dgDic.CurrentPageIndex = 0;
            this.BindDicItemList(1);
        }

        private void btn_ItemPagePrev_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (_ItemCurrentPageIndex == 1) return;
            this.dgDic.CurrentPageIndex = _ItemCurrentPageIndex - 2;
            this.BindDicItemList(_ItemCurrentPageIndex - 1);
        }

        private void btn_ItemPageNext_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (_ItemCurrentPageIndex == _ItemPageCount) return;
            this.dgDic.CurrentPageIndex = _ItemCurrentPageIndex;
            this.BindDicItemList(_ItemCurrentPageIndex + 1);
        }

        private void btn_ItemPageLast_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            this.dgDic.CurrentPageIndex = _ItemPageCount - 1;
            this.BindDicItemList(_ItemPageCount);
        }

        private void btn_ItemGotoPage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (this.txt_ItemPage.Text == string.Empty)
            {
                this.txt_ItemPage.Text = "1";
            }
            int gotopage = 1;
            try
            {
                gotopage = Convert.ToInt32(this.txt_ItemPage.Text);
            }
            catch { }
            int currentpage = 0;

            if (gotopage >= _ItemPageCount)
            {
                gotopage = _ItemPageCount;
                currentpage = gotopage - 1;
                if (currentpage < 0) currentpage = 0;
            }
            else if (gotopage <= 0)
                gotopage = 1;
            else
                currentpage = gotopage - 1;

            this.dgDic.CurrentPageIndex = currentpage;

            this.BindDicItemList(gotopage);

            this.txt_ItemPage.Text = string.Empty;
        }

        #endregion

        #region page navigation item
        private int _ItemPageCount
        {
            get
            {
                if (ViewState["ItemPageCount"] != null) return (int)ViewState["ItemPageCount"];
                else
                    return 0;
            }
            set
            {
                ViewState["ItemPageCount"] = value;
            }
        }
        private int _ItemCurrentPageIndex
        {
            get
            {
                if (ViewState["ItemCurrentPageIndex"] != null) return (int)ViewState["ItemCurrentPageIndex"];
                else
                    return 0;
            }
            set
            {
                ViewState["ItemCurrentPageIndex"] = value;
            }
        }
        private int _ItemRowCount
        {
            get
            {
                if (ViewState["ItemRowCount"] != null) return (int)ViewState["ItemRowCount"];
                else
                    return 0;
            }
            set
            {
                ViewState["ItemRowCount"] = value;
            }
        }
        private void SetPageNavi_Item(int newPage)
        {
            if (newPage <= 1)
            {
                this.btn_ItemPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_first_inact.gif";
                this.btn_ItemPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_pre_inact.gif";
                this.btn_ItemPageFirst.Attributes["disabled"] = "true";
                this.btn_ItemPagePrev.Attributes["disabled"] = "true";
            }
            else
            {
                this.btn_ItemPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_first.gif";
                this.btn_ItemPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_pre.gif";
                this.btn_ItemPageFirst.Attributes.Remove("disabled");
                this.btn_ItemPagePrev.Attributes.Remove("disabled");
            }

            if (newPage >= _ItemPageCount)
            {
                this.btn_ItemPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_next_inact.gif";
                this.btn_ItemPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_last_inact.gif";
                this.btn_ItemPageNext.Attributes["disabled"] = "true";
                this.btn_ItemPageLast.Attributes["disabled"] = "true";
            }
            else
            {
                this.btn_ItemPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_next.gif";
                this.btn_ItemPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path + "/images/pg_last.gif";
                this.btn_ItemPageNext.Attributes.Remove("disabled");
                this.btn_ItemPageLast.Attributes.Remove("disabled");
            }

            this.lbl_ItemCurrentPageIndex.Text = newPage.ToString();
            this.lbl_ItemPageCount.Text = _ItemPageCount.ToString();
            this.lbl_ItemRecordCount.Text = _ItemRowCount.ToString();

        }

        private void SetPageCount_Item(int count, int pageIndex)
        {
            if (count > 0)
            {
                _ItemRowCount = count;
                _ItemPageCount = count / this.dgDic.PageSize;
                if (count % this.dgDic.PageSize > 0) _ItemPageCount++;

                _ItemCurrentPageIndex = pageIndex;
                SetPageNavi_Item(pageIndex);
            }
            else
            {
                _ItemRowCount = count;
                _ItemPageCount = count;
                _ItemCurrentPageIndex = 0;
                SetPageNavi_Item(0);
            }
        }

        #endregion

        private void BindDicType(int pageIndex)
        {
            IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
            IList list = utility.GetDicType();

            IQuery iquery = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
            //			System.Data.DataTable dtDicTypes = iquery.GetDicType();

            //			this.ddlDicType.DataTextField = "DicTypeKey";
            //			this.ddlDicType.DataValueField = "ID";
            //			this.ddlDicType.DataSource = list;
            //			this.ddlDicType.DataBind();

            foreach (BEGDC.CC.Entity.DicType dictype in list)
            {
                if (((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).IsInRole(BEGDC.CC.BL.Common.CODOrOCO.BusinessAdmin) && ((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).IsInRole(BEGDC.CC.BL.Common.CODOrOCO.OCOBusinessAdmin))
                {
                    this.ddlDicType.Items.Add(new System.Web.UI.WebControls.ListItem(dictype.DicTypeKey, dictype.Id.ToString()));
                }
                else
                {
                    if (((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).IsInRole(BEGDC.CC.BL.Common.CODOrOCO.BusinessAdmin))
                    {
                        if (dictype.CODOrOCO == BEGDC.CC.BL.Common.CODOrOCO.OCO)
                            continue;
                    }
                    else if (((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).IsInRole(BEGDC.CC.BL.Common.CODOrOCO.OCOBusinessAdmin))
                    {
                        if (dictype.CODOrOCO == BEGDC.CC.BL.Common.CODOrOCO.COD)
                            continue;
                    }
                    this.ddlDicType.Items.Add(new System.Web.UI.WebControls.ListItem(dictype.DicTypeKey, dictype.Id.ToString()));
                }


            }

            //			IList allType = utility.GetAllDicType();

            //			if (this._Sorting != string.Empty)
            //			{
            //				dtDicTypes.DefaultView.Sort = this._Sorting;
            //			}

            //			this.dgDataType.DataSource = dtDicTypes.DefaultView;
            //			this.dgDataType.DataBind();
            //			this.AllDataType = dtDicTypes;

            //			int count = dtDicTypes.DefaultView.Count;
            //			SetPageCount(count, pageIndex);

            if (list.Count == 0)
                this.BindDicItemList(1);

        }

        private void BindDicItemList(int pageIndex)
        {
            int dictype = 0;
            if (this.ddlDicType.SelectedValue != string.Empty)
                dictype = Convert.ToInt32(this.ddlDicType.SelectedItem.Value);
            //			if(this.ddlDicType.SelectedValue != string.Empty)
            //			{
            //				IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
            //				//=======Phase 2==========/
            //				//Shaly: 1/24/2007
            //				//Use "GetAllDicItem" to get both disabled and enabled items.
            //				//IList list = utility.BuildDicList(this.ddlDicType.SelectedItem.Text);
            //				IList list = utility.GetAllDicItem(dictype);

            IQuery iquery = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
            System.Data.DataTable dtDicItems = iquery.GetDicItemByTypeId(dictype);

            if (this._Sorting_Item != string.Empty)
            {
                dtDicItems.DefaultView.Sort = this._Sorting_Item;
            }

            this.dgDic.DataSource = dtDicItems;
            this.dgDic.DataBind();
            //			this.DataItem = dtDicItems;

            int count = dtDicItems.DefaultView.Count;
            SetPageCount_Item(count, pageIndex);

            //			}

        }

        private void ddlDicType_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            this.dgDic.CurrentPageIndex = 0;
            this.BindDicItemList(1);
        }

        private void btnNewType_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            if (this.txtNewTypeName.Text.Trim() == string.Empty)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.SM001 + "');</script>");
                return;
            }
            BEGDC.CC.Entity.DicType type = new BEGDC.CC.Entity.DicType(this.txtNewTypeName.Text.Trim()
                , this.txtNewTypeName.Text.Trim(), this.chkIsSys.Checked);
            IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
            int result = utility.AddDicType(type);
            if (result == -1)
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.SM002 + "');</script>");
            else if (result == 0)
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CM002 + "');</script>");
            else
            {
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");
                CCIdentity id = this.User.Identity as CCIdentity;
                BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
                log.Module = "DataManagement";
                Employee emp = new Employee();
                emp.Id = id.UserInfo.Id;
                log.Employee = emp;
                log.Time = System.DateTime.Now;
                log.Description = "Add new data type.";
                utility.SaveOperationLog(log);
                this.BindDicType(1);
            }
        }

        void btnNewItem_Click(object sender, EventArgs e)
        {
            if (this.ddlDicType.SelectedValue == string.Empty)
            {
                Page.RegisterClientScriptBlock("", "<script>alert(\"Can't add data item!\");</script>");
                return;
            }

            if (this.txtItemName.Text.Trim() == string.Empty)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.SM006 + "');</script>");
                return;
            }
            int sort = 0;
            if (this.txtSortOrder.Text != string.Empty)
            {
                try
                {
                    sort = Convert.ToInt32(this.txtSortOrder.Text);
                }
                catch
                {
                    Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.SM005 + "');</script>");
                    return;
                }
            }


            BEGDC.CC.Entity.DicType type = new BEGDC.CC.Entity.DicType();
            type.Id = Convert.ToInt32(this.ddlDicType.SelectedValue);
            BEGDC.CC.Entity.DicItem item = new BEGDC.CC.Entity.DicItem
                (this.txtItemName.Text.Trim(), this.txtItemName.Text.Trim(), sort, this.chkIsDefault.Checked, this.chkIsEnabled.Checked, type);

            IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
            int result = utility.AddDicItem(item);
            if (result == -1)
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.SM008 + "');</script>");
            else if (result == 0)
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CM002 + "');</script>");
            else
            {
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");
                CCIdentity id = this.User.Identity as CCIdentity;
                BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
                log.Module = "DataManagement";
                Employee emp = new Employee();
                emp.Id = id.UserInfo.Id;
                log.Employee = emp;
                log.Time = System.DateTime.Now;
                log.Description = "Add new data item.";
                utility.SaveOperationLog(log);
                this.BindDicItemList(1);
            }
        }

        private void dgDic_ItemDataBound(object source, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    string type = e.Item.Cells[5].Text;
                    ImageButton ibItem = (ImageButton)e.Item.FindControl("imageItem");
                    ibItem.Attributes.Add("onclick", "javascript:showItemForm(" + type + ");");
                }
            }
            catch (GDCException gdcEx)
            {
                WebUtility.Go2ErrorPage(this.Page, gdcEx.ErrorNumber);
            }
            catch (Exception ex)
            {
                WebUtility.Go2ErrorPage(this.Page, BEGDC.CC.BL.Common.ErrorCode.Other_Exception);
            }
        }

        private void dgDataType_ItemDataBound(object source, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            try
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    string type = e.Item.Cells[3].Text;
                    ImageButton ibType = (ImageButton)e.Item.FindControl("imageType");
                    ibType.Attributes.Add("onclick", "javascript:showTypeForm(" + type + ");");
                }
            }
            catch (GDCException gdcEx)
            {
                WebUtility.Go2ErrorPage(this.Page, gdcEx.ErrorNumber);
            }
            catch (Exception ex)
            {
                WebUtility.Go2ErrorPage(this.Page, BEGDC.CC.BL.Common.ErrorCode.Other_Exception);
            }
        }

        public void dgDataType_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            if (this._Sorting.IndexOf(e.SortExpression) > -1 && this._Sorting == e.SortExpression)
                this._Sorting = e.SortExpression + " desc";
            else
                this._Sorting = e.SortExpression;
            this.BindDicType(1);

        }

        public void dgDic_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            if (this._Sorting_Item.IndexOf(e.SortExpression) > -1 && this._Sorting_Item == e.SortExpression)
                this._Sorting_Item = e.SortExpression + " desc";
            else
                this._Sorting_Item = e.SortExpression;
            this.BindDicItemList(1);

        }

        void btnExport_Click(object sender, EventArgs e)
        {
            //			if (this.TabStrip1.SelectedIndex == 0)
            //			{
            //				this.dgDataType.AllowPaging = false;
            //				this.BindDicType(1);
            //				BEGDC.CC.Web.Control.ExportDataGridToExcel.Export(Page, this.dgDataType, "DataType.xls");
            //			}
            //			else
            //			{
            this.dgDic.AllowPaging = false;
            this.BindDicItemList(1);
            BEGDC.CC.Web.Control.ExportDataGridToExcel.Export(Page, this.dgDic, "DataItem.xls");
            //			}

        }
    }
}
